import { request } from '../../../../services/request';

// 获取工单列表
export const searchOrderTreeApi = async (params: { current: number; size: number }): Promise<any> => {
    console.log('=== 搜索工单树接口请求 ===');
    console.log('请求参数:', params);
    console.log('======================');

    try {
        const response = await request('app/assignment/searchTree', {
            method: 'POST',
            body: JSON.stringify(params),
        });

        console.log('=== 搜索工单树接口返回 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 搜索工单树接口异常 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

export const getCanAssignmentTaskApi = async (batchOrderId: string): Promise<any> => {
    console.log('=== 获取可分配任务接口请求 ===');
    console.log('请求参数:', { batch_order_id: batchOrderId });
    console.log('======================');
    try {
        const response = await request('app/assignment/getCanAssignmentTask', {
            method: 'POST',
            body: JSON.stringify({
                batch_order_id: batchOrderId
            }),
        });
        console.log('=== 获取可分配任务接口返回 ===');
        console.log('响应数据:', response);
        console.log('======================');
        return response;
    } catch (error) {
        console.error('=== 获取可分配任务接口异常 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 获取gantt资源和任务
export const getTaskResourceOccupyInfoApi = async (taskId: string): Promise<any> => {
    console.log('=== 获取gantt资源和任务接口请求 ===');
    console.log('请求参数:', { id: taskId });
    console.log('======================');

    try {
        const response = await request('app/assignment/getTaskResourceOccupyInfo', {
            method: 'POST',
            body: JSON.stringify({
                id: taskId
            }),
        });

        console.log('=== 获取gantt资源和任务接口返回 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 获取gantt资源和任务接口异常 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};

// 派工接口
export const assignmentTaskApi = async (params: {
    id: string;
    capacity: number;
    batch_order_num: string;
    work_start: string;
    work_end: string;
    work_device_id?: string;
    work_station_id?: string;
    work_user_id?: string;
    work_line_id?: string;
}): Promise<any> => {
    console.log('=== 派工接口请求 ===');
    console.log('请求参数:', params);
    console.log('======================');

    try {
        const response = await request('app/assignment/assignmentTask', {
            method: 'POST',
            body: JSON.stringify(params),
        });

        console.log('=== 派工接口返回 ===');
        console.log('响应数据:', response);
        console.log('======================');

        return response;
    } catch (error) {
        console.error('=== 派工接口异常 ===');
        console.error('错误信息:', error);
        console.error('======================');
        throw error;
    }
};